﻿using System.Collections.Generic;
using System.Linq;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.BusinessObjects;
using com.eforceglobal.crux.BusinessObjects.DatabaseQueries;

namespace com.eforceglobal.crux.BusinessFacades
{
    public class RoleFacade
    {

        public List<RoleInformation> GetAllRoles(int companyid)
        {
            Queries queries = new Queries();
            IEnumerable<RoleInformation> roles = from rolelist in queries.GetRoles(companyid)
                                          select ClassConverter.CopyRoleToRoleInformation(rolelist);
            return roles.ToList();
        }

        public List<RoleInformation> GetNonSuperAdminisativeRoles(int companyid)
        {
            //TODO: Don't Hard-Code Privilage Names. Make Them Configurable.
            //TODO:Saving Of Own Preferences And Roles Is Not Being Allowed.
            // Saving Should be allowed In A Secured Way.

            Queries queries = new Queries();
            IEnumerable<RoleInformation> roles = from rolelist in queries.GetRolesNotHavingPrivilage(companyid, "SuperAdministrative")
                                                 select ClassConverter.CopyRoleToRoleInformation(rolelist);
            return roles.ToList();
        }

        public List<RoleInformation> GetRolesForUser(int userid)
        {
            Queries queries = new Queries();
            IEnumerable<RoleInformation> roles = from rolelist in queries.GetRolesForUser(userid)
                                                 select ClassConverter.CopyRoleToRoleInformation(rolelist);
            return roles.ToList();
        }

        public List<RoleInformation> GetAdministrativeRoles()
        {
            return GetRolesByPrivilage("Administrative");
        }

        public List<RoleInformation> GetSuperAdministativeRoles()
        {
            return GetRolesByPrivilage("SuperAdministrative");
        }

        public List<RoleInformation> GetRolesByPrivilage(string PrivilageName)
        {
            Queries queries = new Queries();
            IEnumerable<RoleInformation> roleInformations = from rolelist in queries.GetRoles(PrivilageName)
                                                            select ClassConverter.CopyRoleToRoleInformation(rolelist);
            return roleInformations.ToList();
        }

        public List<string> GetRoleNamesByPrivilage(string PrivilageName)
        {
            return GetRolesByPrivilage(PrivilageName).Select(p => p.RoleName).ToList();
        }

        
    }
}
